import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
  console.log('开始初始化数据库...')

  // 创建测试用户
  const testUser = await prisma.user.upsert({
    where: { email: 'admin@test.com' },
    update: {},
    create: {
      loginId: 'admin',
      email: 'admin@test.com',
      passwordHash: await bcrypt.hash('123456', 10),
      name: '管理员',
      nickname: '管理员',
      role: 'admin',
      status: 'active',
    },
  })

  console.log('创建测试用户:', testUser.email)

  // 创建测试频道
  const defaultChannel = await prisma.channel.upsert({
    where: { id: BigInt(1) },
    update: {},
    create: {
      id: BigInt(1),
      name: '默认频道',
      description: '这是一个默认的测试频道',
      ownerUserId: testUser.id,
      avatarUrl: null,
    },
  })

  const testChannel = await prisma.channel.upsert({
    where: { id: BigInt(2) },
    update: {},
    create: {
      id: BigInt(2),
      name: '测试频道',
      description: '这是一个测试频道，用于演示功能',
      ownerUserId: testUser.id,
      avatarUrl: null,
    },
  })

  console.log('创建测试频道:', defaultChannel.name, testChannel.name)

  console.log('数据库初始化完成！')
  console.log('测试账号: admin@test.com / 123456')
}

main()
  .then(async () => {
    await prisma.$disconnect()
  })
  .catch(async (e) => {
    console.error(e)
    await prisma.$disconnect()
    process.exit(1)
  })
